package com.bs.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class DataTable{

    private static String SCAN_TIME = "SCAN_TIME";
    private DataRowCollection rows; // 用于保存DataRow的集合对象
    private DataColumnCollection columns; // 用于保存DataColumn的对象
    private String tableName; // 表名
    private String tableLocalName; // 表中文名
    private String dataSourceName; // 数据源名
    private DataKey primeKey = new DataKey();// 主键
    private List<DataKey> dataIndexs = new ArrayList<DataKey>();// 数据索引列表
    private Map<String, Object> tag = new HashMap<String, Object>(); // 做扩展用，（比如TOPN那边如果分页的话是用来作为分页使用的）

    public DataKey getPrimeKey() {
        return primeKey;
    }

    public void setPrimeKey(DataKey primeKey) {
        this.primeKey = primeKey;
    }

    public List<DataKey> getDataIndexs() {
        return dataIndexs;
    }

    public void setDataIndexs(List<DataKey> dataIndexs) {
        this.dataIndexs = dataIndexs;
    }

    public DataTable() {
        this.columns = new DataColumnCollection();
        this.rows = new DataRowCollection();
        this.rows.setColumns(columns);
    }

    public DataTable(String dataTableName) {
        this();
        this.tableName = dataTableName;
    }

    public int getTotalCount() {
        return rows.size();
    }

    /**
     * 功能描述： 返回表名
     *
     * @param
     * @return: String
     * @author: guojiyong
     * @version: 2.0
     */
    public String getTableName() {
        return this.tableName;
    }

    /**
     * 功能描述： 设置表名
     *
     * @param
     * @return: void
     * @author: guojiyong
     * @version: 2.0
     */
    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    /**
     * 功能描述： 返回该表引用的封装类
     *
     * @param
     * @return: DataRowCollection
     * @author: guojiyong
     * @version: 2.0
     */
    public DataRowCollection getRows() {
        return this.rows;
    }

    public DataColumnCollection getColumns() {
        return this.columns;
    }

    /**
     * 功能描述： 获取指定行指定列的数据
     *
     * @param
     * @return: Object
     * @author: James Cheung
     * @version: 2.0
     */

    public Object getValue(int row, String colName) {
        return this.rows.get(row).getValue(colName);
    }

    public Object getValue(int row, int col) {
        return this.rows.get(row).getValue(col);
    }

    /**
     * 功能描述： 为该表数据新建一行
     *
     * @param
     * @return: DataRow
     * @author: guojiyong
     * @version: 2.0
     */
    public DataRow newRow() throws Exception {
        DataRow tempRow = new DataRow(this);
        // nextRowIndex = nextRowIndex < this.rows.size() ? this.rows.size()
        // : nextRowIndex;
        int lastRowIndex = 0;
        if (this.rows.size() > 0) {
            lastRowIndex = this.rows.get(this.rows.size() - 1).getRowIndex();
        } else {
            lastRowIndex = 0;
        }

        tempRow.setColumns(this.columns);
        tempRow.setRowIndex(++lastRowIndex);
        return tempRow;
    }

    public void setValue(int row, int col, Object value) {
        this.rows.get(row).setValue(col, value);
    }

    public void setValue(int row, String colName, Object value) {
        this.rows.get(row).setValue(colName, value);
    }

    /**
     * @param name
     */
    public void setTag(String name, Object value) {
        this.tag.put(name, value);
    }

    /**
     * @return the tag
     */
    public Object getTag(String name) {
        return tag.get(name);
    }

    public DataColumn addColumn(String columnName, String dataType) throws Exception {
        return this.columns.addColumn(columnName, dataType);
    }

    public DataColumn addColumnIndex(int index, String columnName, String dataType) throws Exception {
        return this.columns.addColumn(index, columnName, dataType);

    }

    public boolean addRow(DataRow row) throws Exception {
        // if (row.getRowIndex() > this.rows.size())
        // row.setRowIndex(this.rows.size());
        // return this.rows.add(row);

        if (this.rows.size() > 0) {
            // if (row.getRowIndex() >
            // this.rows.get(this.rows.size()-1).getRowIndex() + 1) {
            row.setRowIndex(this.rows.get(this.rows.size() - 1).getRowIndex() + 1);
            // }
        } else {
            row.setRowIndex(1);
        }
        return this.rows.add(row);

    }

    public boolean addRow(Object... objects) throws Exception{
        DataRow row = newRow();
        for(int i = 0; i < objects.length; i++){
            row.setValue(i,objects[i]);
        }
        this.addRow(row);
        return true;
    }

    public DataTable cloneTable() {
        try {
            DataTable table = new DataTable();
            table.setTableName(this.getTableName());
            table.setDataSourceName(this.getDataSourceName());
            table.setTableLocalName(this.getTableLocalName());
            table.setPrimeKey(primeKey);
            table.setDataIndexs(getDataIndexs());
            for (DataColumn dc : this.columns) {
                DataColumn dcc = table.addColumn(dc.getColumnName(), dc.getDataType());
                dcc.setDisplayed(dc.isDisplayed());
            }
            return table;
        } catch (Exception ex) {
            return null;
        }
    }
    /**
     * @return the tableLocalName
     */
    public String getTableLocalName() {
        return tableLocalName;
    }

    /**
     * @param tableLocalName
     */
    public void setTableLocalName(String tableLocalName) {
        this.tableLocalName = tableLocalName;
    }

    /**
     * @return the dataSourceName
     */
    public String getDataSourceName() {
        return dataSourceName;
    }

    /**
     * @param dataSourceName
     */
    public void setDataSourceName(String dataSourceName) {
        this.dataSourceName = dataSourceName;
    }
}
